/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serwer;

import Serializable.ParsedCmd;

/**
 *
 * @author nicra
 */
public class Protocol {
    
    
    
    public static ParsedCmd process(ParsedCmd pC,UserState uS){
        ParsedCmd out=null;
        pC.from=uS.ID;
        switch(pC.mainCmd){
            case "HAI":
                out=new ParsedCmd("HAI");
                break;
            case "ID PLZ":
                uS.loginState=UserState.HAS_ID;
                uS.ID=Serwer.cList.connect();
                out=new ParsedCmd("UID",uS.ID);                
                break;  
            case "RLOGIN":
                if(uS.loginState!=UserState.HAS_ID){
                    uS.loginErr();
                    out=new ParsedCmd("BAD");
                    break;
                }
                if(!Serwer.bManager.base.isUser(pC.stringParam[0])){
                    uS.nick=pC.stringParam[0];
                    uS.loginState=UserState.IS_LOGIN;
                    out=new ParsedCmd("LOGINOK");
                }else{
                    uS.loginErr();
                    out=new ParsedCmd("LOGINBAD");
                }
                break;
            case "RPASS":
                if(uS.loginState!=UserState.IS_LOGIN){
                    uS.loginErr();
                    out=new ParsedCmd("BAD");
                    break;
                }
                if(Serwer.bManager.base.addUser(uS.nick, pC.stringParam[0])){
                    uS.pass=pC.stringParam[0];
                    uS.loginState=UserState.IS_PASS;
                    out=new ParsedCmd("PASSOK");
                }else{
                    uS.loginErr();
                    out=new ParsedCmd("PASSBAD");
                }
                break;
            case "LOGIN":
                if(uS.loginState!=UserState.HAS_ID){
                    uS.loginErr();
                    out=new ParsedCmd("BAD");
                    break;
                }
                if(Serwer.bManager.base.isUser(pC.stringParam[0])){
                    uS.nick=pC.stringParam[0];
                    uS.loginState=UserState.IS_LOGIN;
                    out=new ParsedCmd("LOGINOK");
                }
                else {
                    uS.loginErr();
                    out=new ParsedCmd("LOGINBAD");
                }
                break;
            case "NGAME":{
                Serwer.games.add(new GameInfo(pC.stringParam[0],uS,pC.intParam[0]));
                uS.inGame=Serwer.games.get(Serwer.games.size()-1);
                out=new ParsedCmd("OK"); 
                break;
            }
            case "JOIN":
                for(int i=0;i<Serwer.games.size();++i){
                    if(Serwer.games.get(i).gameName.equals(pC.stringParam[0])){
                        if(Serwer.games.get(i).join(uS)){
                            uS.inGame=Serwer.games.get(Serwer.games.size()-1);
                            out=new ParsedCmd("OK");
                        }
                        break;
                    }
                }
                if(out==null) out = new ParsedCmd("NOPE");
                break;
            case "DISCONNECT":
                uS.inGame.disconnect(uS);
                out = new ParsedCmd("OK");
                break;
            case "GETSLIST":{
                out=new ParsedCmd("SLIST");
                out.intParam=new int[Serwer.games.size()*2+1];
                out.intParam[0]=Serwer.games.size();
                out.stringParam = new String[Serwer.games.size()];
                for(int i=0;i<Serwer.games.size();++i){
                    out.stringParam[i]=Serwer.games.get(i).gameName;
                    out.intParam[i*2+1]=Serwer.games.get(i).maxPlayers; 
                    out.intParam[i*2+2]=Serwer.games.get(i).players();
                }
                break;
            }
            case "READY":{
                uS.inGame.ready(uS);
                out=new ParsedCmd("OK");
                break;
            }
            case "NREADY":{
                uS.inGame.notReady(uS);
                out=new ParsedCmd("OK");
                break;
            }
            case "GETPLIST":{
                out=new ParsedCmd("PLIST");
                out.stringParam = new String[uS.inGame.playersN.length];
                out.intParam = new int[uS.inGame.playersN.length];
                for(int i=0;i<uS.inGame.maxPlayers;++i){
                    out.stringParam[i]=uS.inGame.playersN[i];
                    out.intParam[i]=(uS.inGame.ready[i])?1:0;
                }
                break;
            }                
            case "PASS":
                if(uS.loginState!=UserState.IS_LOGIN){
                    uS.loginErr();
                    out=new ParsedCmd("BAD");
                    break;
                }
                if(Serwer.bManager.base.checkPass(uS.nick, pC.stringParam[0])){
                    uS.pass=pC.stringParam[0];
                    uS.loginState=UserState.IS_PASS;
                    out=new ParsedCmd("PASSOK");
                }
                else {
                    uS.loginErr();
                    out=new ParsedCmd("PASSBAD");
                }
                break;
            case "LOADED":
            case "NTOWER":
            case "DMOB":
            case "NMOB":
            case "DMG":
                uS.inGame.from(pC);
                out=new ParsedCmd();
                    break;
            default:
                out=new ParsedCmd();             
        }
        return out;
    }
}
